package com.jiantong.work.impl.service.yangzhibaoxian;

import com.google.common.base.Throwables;
import com.jiantong.work.impl.dao.yangzhibaoxian.InsuranceApplicationDao;
import com.jiantong.work.impl.dao.yangzhibaoxian.InsuranceRetailDao;
import com.jiantong.work.model.yangzhibaoxian.InsuranceRetail;
import com.jiantong.work.service.yangzhibaoxian.InsuranceRetailService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by ThinkPad on 2017/9/5.
 */
@Slf4j
@Service
@RpcProvider
public class InsuranceRetailServiceImpl implements InsuranceRetailService {
    private InsuranceRetailDao insuranceRetailDao;

    @Autowired
    public InsuranceRetailServiceImpl(InsuranceRetailDao insuranceRetailDao) {
        this.insuranceRetailDao = insuranceRetailDao;
    }

    @Override
    public Response<InsuranceRetail> findById(Long id) {
        try {
            InsuranceRetail insuranceRetail = (InsuranceRetail) insuranceRetailDao.findById(id);
            if (insuranceRetail == null) {
                log.error("insuranceRetail(id={}) is not found", id);
                return Response.fail("insuranceRetail.not.found");
            }

            return Response.ok(insuranceRetail);
        } catch (Exception e) {
            log.error("failed to find insuranceRetail id={}), cause:{}", id, Throwables.getStackTraceAsString(e));
            return Response.fail("insuranceRetail.find.fail");
        }
    }

    @Override
    public Response<List<InsuranceRetail>> findByIds(List ids) {
        try {
            List<InsuranceRetail> insuranceRetailList = insuranceRetailDao.findByIds(ids);
            if (insuranceRetailList == null) {
                log.error("insuranceRetailList(ids={}) is not found", ids);
                return Response.fail("insuranceRetailList.not.found");
            }

            return Response.ok(insuranceRetailList);
        } catch (Exception e) {
            log.error("failed to find insuranceRetailList(ids={}), cause:{}", ids, Throwables.getStackTraceAsString(e));
            return Response.fail("insuranceRetailList.find.fail");
        }

    }

    @Override
    public Response<List<InsuranceRetail>> list(Map map) {
        try {
            List<InsuranceRetail> insuranceRetailList = insuranceRetailDao.list(map);
            if (insuranceRetailList == null) {
                log.error("insuranceRetailList(ids={}) is not found", map);
                return Response.fail("insuranceRetailList.not.found");
            }

            return Response.ok(insuranceRetailList);
        } catch (Exception e) {
            log.error("failed to find insuranceRetailList(ids={}), cause:{}", map, Throwables.getStackTraceAsString(e));
            return Response.fail("insuranceRetailList.find.fail");
        }
    }

    @Override
    public Response<Paging<InsuranceRetail>> ping(Integer offset, Integer limit, Map map) {
        try {
            PageInfo pageInfo = new PageInfo(offset, limit);
            Paging<InsuranceRetail> insuranceRetailPaging = insuranceRetailDao.paging(pageInfo.getOffset(), pageInfo.getLimit(), map);
            if (insuranceRetailPaging == null) {
                log.error("insuranceRetailPaging(ids={}) is not found", map);
                return Response.fail("insuranceRetailPaging not.found");
            }

            return Response.ok(insuranceRetailPaging);
        } catch (Exception e) {
            log.error("failed to find insuranceRetailPaging(ids={}), cause:{}", map, Throwables.getStackTraceAsString(e));
            return Response.fail("insuranceRetailPaging.find.fail");
        }
    }

    @Override
    public Boolean update(InsuranceRetail insuranceRetail) {
        try {
            Boolean b = insuranceRetailDao.update(insuranceRetail);
            return b;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public Long create(InsuranceRetail insuranceRetail) {
        try {
            insuranceRetailDao.create(insuranceRetail);
            return insuranceRetail.getId();
        } catch (Exception e) {
            e.printStackTrace();
            return -500L;
        }
    }

    @Override
    public Integer creates(List<InsuranceRetail> insuranceRetailList) {
        try {

            return insuranceRetailDao.creates(insuranceRetailList);
        } catch (Exception e) {
            e.printStackTrace();
            return -500;
        }
    }

}
